﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Npgsql;
using System.Data;
using System.Windows.Forms;
using SmartMD.DataAccess.Oracle;

namespace SmartMD.DataAccess.Postgresql
{
    public class DbConnection : IConnection
    {
        public DbConnection()
        { 
        }
        private NpgsqlConnection conn = null;
        private static DbConnection inst = null;
        public static DbConnection getInstance()
        {
            if (inst == null)
            {
                inst = new DbConnection();
            }
            return inst;
        }

        public void setConnetion(string DataString)
        {
            if (DataString == null)
                return;

            string connectionString = DataString;//"Server=127.0.0.1;Port=5432;User Id=postgres;Password=duo_platform;Database=postgres;";
            conn = new NpgsqlConnection(connectionString);
            // 打开一个数据库连接，在执行相关SQL之前调用
            conn.Open();
        }
        /**
         * 功能：关系连接
         * 
         * */
        public void closeConnetion()
        {
            if (conn != null)
            {
                conn.Close();
                conn = null;
            }
        }

        public DataTable Select(string sql)
        {
            DataTable dt = new DataTable();
            try
            {
                if (conn == null)
                    return null;
                NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(sql, conn);
                adapter.Fill(dt);
                return dt;
            }
            catch (Exception ep)
            {
                MessageBox.Show(ep.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return dt;
            }
        }

        public Boolean Insert(string sql)
        {
            try
            {
                if (conn == null)
                    return false;
                NpgsqlCommand pgCmd = new NpgsqlCommand(sql, conn);
                int d = pgCmd.ExecuteNonQuery();
                if (d < 1)
                    return false;
                return true;
            }
            catch (Exception ep)
            {
                MessageBox.Show(ep.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        public Boolean Update(string sql)
        {
            try
            {
                if (conn == null)
                    return false;
                NpgsqlCommand oledbCom = new NpgsqlCommand(sql, conn);
                int d = oledbCom.ExecuteNonQuery();
                if (d < 1)
                    return false;
                return true;
            }
            catch (Exception ep)
            {
                MessageBox.Show(ep.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        public Boolean Delete(string sql)
        {

            XMLHelper xmlOper = new XMLHelper();
            String dataAccess = xmlOper.GetXmlNodeAttribute("SmartMD.Manager.vshost.exe.config", "configuration/appSettings/add[@key='PostgresqlAddress']", "value");
            if (dataAccess == null)
                return false;
            NpgsqlConnection conn = new NpgsqlConnection(dataAccess);
            conn.Open();
            try
            {
                if (conn == null)
                    return false;
                NpgsqlCommand oledbCom = new NpgsqlCommand(sql, conn);
                int d = oledbCom.ExecuteNonQuery();
                if (d < 1)
                    return false;
                return true;
            }
            catch (Exception ep)
            {
                MessageBox.Show(ep.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
